import numpy as np
from fuzzywuzzy import fuzz

class 韵律探针:
    def __init__(self, 量子叠加=False):
        self.量子叠加 = 量子叠加
        self.韵律矩 = None

    def 探韵(self, 文):
        句列 = 文.split('\n')
        末字列 = [句.strip()[-1] for 句 in 句列 if 句.strip()]
        
        if len(set(末字列)) == 1:
            return f"押{末字列[0]}韵"
        else:
            if self.量子叠加:
                韵集 = set(末字列)
                return f"量子韵态：{'、'.join(韵集)}"
            else:
                return "自由韵"

    def 建韵律矩(self, 文):
        句列 = 文.split('\n')
        末字列 = [句.strip()[-1] for 句 in 句列 if 句.strip()]
        self.韵律矩 = np.zeros((len(末字列), len(末字列)))
        
        for i in range(len(末字列)):
            for j in range(len(末字列)):
                self.韵律矩[i, j] = fuzz.ratio(末字列[i], 末字列[j])
        return self.韵律矩

    def 析韵矩(self):
        if self.韵律矩 is None:
            return "韵律矩阵未立"
        本征值, 本征向量 = np.linalg.eig(self.韵律矩)
        主韵序 = np.argmax(本征值)
        return f"主韵在序：{主韵序}"

class 韵律析仪:
    def __init__(self):
        self.韵谱 = {}

    def 解韵谱(self, 文):
        句列 = 文.split('\n')
        末字列 = [句.strip()[-1] for 句 in 句列 if 句.strip()]
        
        for 行序, 末字 in enumerate(末字列):
            if 末字 not in self.韵谱:
                self.韵谱[末字] = [行序]
            else:
                self.韵谱[末字].append(行序)
        return self.韵谱

    def 析韵构(self):
        if not self.韵谱:
            return "韵谱未立"
        韵构 = {}
        for 韵字, 位列 in self.韵谱.items():
            韵构[韵字] = len(位列)
        return 韵构

class 韵律编译仪:
    def __init__(self):
        self.韵码列 = []

    def 编译韵式(self, 文):
        句列 = 文.split('\n')
        末字列 = [句.strip()[-1] for 句 in 句列 if 句.strip()]
        
        for 行序, 末字 in enumerate(末字列):
            self.韵码列.append(f"Rhyme[{行序}] = '{末字}'")
        return "\n".join(self.韵码列)

    def 执韵码(self):
        for 韵码 in self.韵码列:
            print(韵码)
